package com.zj.utils.utils

import android.annotation.SuppressLint
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern

/**
 * 时间格式转换工具类
 * @author jiang zhu on 2019/4/18
 */
class TimeUtils{

    companion object {
        /**
         * @time Long
         * @format yyyy-MM-dd HH:mm:ss
         */
        fun timeStampNYR(time: Long, format: String?): String {
            var format = format
            if (format == null || format.isEmpty()) {
                format = "yyyy-MM-dd"
            }
            val sdf = SimpleDateFormat(format)
            return sdf.format(Date(time))
        }
        fun timeStamp2Date(time: Long, format: String?): String {
            var format = format
            if (format == null || format.isEmpty()) {
                format = "yyyy-MM-dd HH:mm:ss"
            }
            val sdf = SimpleDateFormat(format)
            return sdf.format(Date(time))
        }

        /**
         * 字符日期转换时间戳
         */
        @SuppressLint("SimpleDateFormat")
        fun string2TimeStamp(timeString: String, pattern: String?): Long{
            var pattern1 = pattern
            if(null == pattern) pattern1 = "yyyy-MM-dd HH:mm:ss";
            return SimpleDateFormat(pattern1).parse(timeString).time
        }
        fun getFileName(pathandname: String): String? {

                val start = pathandname.lastIndexOf("/")
                val end = pathandname.length
        return if (start != -1 && end != -1) {
            pathandname.substring(start + 1, end)
        } else {
            null
        }

        }
        fun  getPicSuffix( img_path:String):String?{
        if (img_path == null || img_path.indexOf(".") == -1){
            return ""; //如果图片地址为null或者地址中没有"."就返回""
        }
        return img_path.substring(img_path.lastIndexOf(".") + 1).
                trim().toLowerCase();
        }
        /**
         * 根据提供的年月日获取该月份的第一天
         * @Description: (这里用一句话描述这个方法的作用)
         * @Author: gyz
         * @Since: 2017-1-9下午2:26:57
         * @param year
         * @param monthOfYear
         * @return
         */
        fun  getSupportBeginDayofMonth(year:Int, monthOfYear:Int):Date? {
                var cal = Calendar.getInstance()
                // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
                cal.set(Calendar.YEAR, year)
                cal.set(Calendar.MONTH, monthOfYear)
                cal.set(Calendar.DAY_OF_MONTH, 1)
                cal.set(Calendar.HOUR_OF_DAY, 0)
                cal.set(Calendar.MINUTE, 0)
                cal.set(Calendar.SECOND, 0)

                cal.add(Calendar.DAY_OF_MONTH, -1)
                var lastDate = cal.time

                cal.set(Calendar.DAY_OF_MONTH, 1)
                var firstDate = cal.getTime()
        return firstDate
        }
        var workSafeEndTime = ""
        var workSafeStartTime = ""
        var dateStr = ""
        var formart = SimpleDateFormat("yyyy-MM-dd")
        var starYear = 0
        var starMoth = 0
        private var mMothData = ArrayList<String>()
        fun  getSupportBeginDayofMonthMy():String? {
                mMothData.clear()
                mMothData.add("1")
                mMothData.add("2")
                mMothData.add("3")
                mMothData.add("4")
                mMothData.add("5")
                mMothData.add("6")
                mMothData.add("7")
                mMothData.add("8")
                mMothData.add("9")
                mMothData.add("10")
                mMothData.add("11")
                mMothData.add("12")
                //TODO 王哥头部哦
                var simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
                var date = Date(System.currentTimeMillis())
                dateStr = simpleDateFormat.format(date)
                var substring = dateStr.substring(0, dateStr.length - 3)
                //自己定义
                var dfYM = SimpleDateFormat("yyyy")
                var dfYString = dfYM.format(Date())
                var dfMOTH = SimpleDateFormat("MM")
                var dfYMOth = dfMOTH.format(Date())
                starYear = dfYString.toInt()
        for (index in 0 until mMothData.size) {
            if (dfYMOth.contains(mMothData[index])) {
                starMoth = dfYMOth.toInt()
            }
        }
        var cal = Calendar.getInstance()
        // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
        cal.set(Calendar.YEAR, starYear)
        cal.set(Calendar.MONTH, starMoth)
        cal.set(Calendar.DAY_OF_MONTH, 1)
        cal.set(Calendar.HOUR_OF_DAY, 0)
        cal.set(Calendar.MINUTE, 0)
        cal.set(Calendar.SECOND, 0)

        cal.add(Calendar.DAY_OF_MONTH, -1)
        var lastDate = cal.time

        cal.set(Calendar.DAY_OF_MONTH, 1)
        var firstDate = cal.getTime()
        workSafeStartTime = formart.format(firstDate)
        return workSafeStartTime
        }
        /**
         * 根据提供的年月获取该月份的最后一天
         * @Description: (这里用一句话描述这个方法的作用)
         * @Author: gyz
         * @Since: 2017-1-9下午2:29:38
         * @param year
         * @param monthOfYear
         * @return
         */
        fun  getSupportEndDayofMonthMy():String{
            mMothData.clear()
            mMothData.add("1")
            mMothData.add("2")
            mMothData.add("3")
            mMothData.add("4")
            mMothData.add("5")
            mMothData.add("6")
            mMothData.add("7")
            mMothData.add("8")
            mMothData.add("9")
            mMothData.add("10")
            mMothData.add("11")
            mMothData.add("12")
            //TODO 王哥头部哦
            var simpleDateFormat = SimpleDateFormat("yyyy-MM-dd")
            var date = Date(System.currentTimeMillis())
            dateStr = simpleDateFormat.format(date)
            var substring = dateStr.substring(0, dateStr.length - 3)
            //自己定义
            var dfYM = SimpleDateFormat("yyyy")
            var dfYString = dfYM.format(Date())
            var dfMOTH = SimpleDateFormat("MM")
            var dfYMOth = dfMOTH.format(Date())
            starYear = dfYString.toInt()
            for (index in 0 until mMothData.size) {
                if (dfYMOth.contains(mMothData[index])) {
                    starMoth = dfYMOth.toInt()
                }
            }


            var cal = Calendar.getInstance();
            // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
            cal.set(Calendar.YEAR, starYear)
            cal.set(Calendar.MONTH, starMoth)
            cal.set(Calendar.DAY_OF_MONTH, 1)
            cal.set(Calendar.HOUR_OF_DAY, 23)
            cal.set(Calendar.MINUTE, 59)
            cal.set(Calendar.SECOND, 59)

            cal.add(Calendar.DAY_OF_MONTH, -1)
            var lastDate = cal.time

            cal.set(Calendar.DAY_OF_MONTH, 1)
            var firstDate = cal.time
            workSafeEndTime = formart.format(firstDate)

            return workSafeEndTime;
        }
        /**
         * 根据提供的年月获取该月份的最后一天
         * @Description: (这里用一句话描述这个方法的作用)
         * @Author: gyz
         * @Since: 2017-1-9下午2:29:38
         * @param year
         * @param monthOfYear
         * @return
         */
        fun  getSupportEndDayofMonth(year:Int,monthOfYear:Int):Date{
            var cal = Calendar.getInstance();
            // 不加下面2行，就是取当前时间前一个月的第一天及最后一天
            cal.set(Calendar.YEAR, year)
            cal.set(Calendar.MONTH, monthOfYear)
            cal.set(Calendar.DAY_OF_MONTH, 1)
            cal.set(Calendar.HOUR_OF_DAY, 23)
            cal.set(Calendar.MINUTE, 59)
            cal.set(Calendar.SECOND, 59)

            cal.add(Calendar.DAY_OF_MONTH, -1)
            var lastDate = cal.time

            cal.set(Calendar.DAY_OF_MONTH, 1)
            var firstDate = cal.time
            return lastDate;
        }
        /**
         * 将paramValue中的汉字提取出来
         * @param paramValue
         * @return
         */
        fun  getChinese( paramValue:String):String {
            var str = "";
            var regex = "([\u4e00-\u9fa5]+)";
            var matcher = Pattern.compile(regex).matcher(paramValue);
            while (matcher.find()) {
                str += matcher.group(0)
            }
            return str
        }
        fun getTimeTodyMonier():Long{
            val nowTime = System.currentTimeMillis()
            return nowTime - (nowTime + TimeZone.getDefault().rawOffset) % (1000 * 3600 * 24)
        }

        fun getNowBeforMothTimeString():String{
            val curr = Calendar.getInstance()
            curr.set(Calendar.MONTH, curr.get(Calendar.MONTH)-1)
            val date = curr.time
            return formart.format(date)
        }
        fun getNowTimeString():String{
            val curr = Calendar.getInstance()
            curr.set(Calendar.MONTH, curr.get(Calendar.MONTH))
            val date = curr.time
            return formart.format(date)
        }
    }


}
